--
-- Created by IntelliJ IDEA.
-- User: linux
-- Date: 2022/1/9
-- Time: 16:13
-- To change this template use File | Settings | File Templates.
--

-- 基本数据类型
local name = 'godchild'
local age = 18

-- nil
local sex
ngx.say(type(sex))

if sex then
    ngx.say(name)
else
    ngx.say(age)
end

local PI = 3.14159
ngx.say(PI)
ngx.say(math.floor(PI))
ngx.say(math.ceil(PI))

local test = [==[~~~[].\/~~~~~~~\n~~~~~~~]==]
ngx.say(test)
--
--

local function change(arg)
    -- change 函数，改变长方形的长和宽，使其各增长一倍
    arg.width = arg.width * 2
    arg.height = arg.height * 2
    return arg
end

local rectangle = { width = 20, height = 15 }
change(rectangle)
ngx.say(rectangle.height .. ' ' .. rectangle.width)


-- 导入自定义模块

local my_module = require("mylua")
local msg = my_module.greeting()
ngx.say(msg)

-- string 库

ngx.say(string.byte("abc"))
ngx.say(string.char(97, 98, 99))
ngx.say(string.upper("abcd"))
ngx.say(string.lower("ABCD"))
ngx.say(string.len("abcd"))  -- 不推荐
ngx.say("使用 # 操作符计算字符串长度 'abcd': " .. #"abcd")
ngx.say(string.find("what your name?", "our"))

-- 日期时间

ngx.say(ngx.today())
ngx.say(ngx.time())
ngx.say(ngx.now())
ngx.say(ngx.localtime())
ngx.say(ngx.utctime())

ngx.update_time()

-- ngx.log(ngx.ERR, ngx.today())


ngx.say("--------------------------------")

function get_from_cache(key)
    local cache_ngx = ngx.shared.my_cache
    local value = cache_ngx:get(key)
    return value
end

function set_to_cache(key, value, exptime)
    if not exptime then
        exptime = 0
    end

    local cache_ngx = ngx.shared.my_cache
    local succ, err, forcible = cache_ngx:set(key, value, exptime)
    return succ
end

set_to_cache('name', 'aaron', 60)

local my_value = get_from_cache('name')
ngx.say(my_value)
